/*
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <cstdio>
#include "It_process_fs_test.h"

#define MAX_CONTAINER_NUM 5
#define CONFIG_LEN        16

static std::string arryEntries[MAX_CONTAINER_NUM] = {
    "max_mnt_container",
    "max_pid_container",
    "max_user_container",
    "max_net_container",
    "max_uts_container"
};

static int nInitArry[MAX_CONTAINER_NUM] = {61, 54, 49, 44, 41};

UINT32 WriteContainer(const char *filepath, int value)
{
    PRINTTEST("writeproc %d >> %s\n", value, filepath);
    int fd = open(filepath, O_WRONLY);
    char buf[CONFIG_LEN];
    sprintf(buf, "%d", value);
    size_t twd = write(fd, buf, (strlen(buf)+1));
    close(fd);
    return twd;
}

UINT32 ReadContainer(std::string strFile, int value)
{
    char szStatBuf[CONFIG_LEN];
    FILE *fp;
    fp = fopen(strFile.c_str(), "rb");
    ICUNIT_ASSERT_NOT_EQUAL(fp, 0, fp);

    int ret;
    memset(szStatBuf, 0, CONFIG_LEN);
    ret = fread(szStatBuf, 1, CONFIG_LEN, fp);
    PRINTTEST("cat %s\n", strFile.c_str());

    PRINTTEST("%s\n", szStatBuf);
    ret = atoi(szStatBuf);
    ICUNIT_ASSERT_EQUAL(ret, value, ret);

    fclose(fp);
    return 0;
}

void ErrWriteContainer0(const char *filepath)
{
    int fd = open(filepath, O_WRONLY);
    char buf[CONFIG_LEN];
    sprintf(buf, "%d", 0);
    PRINTTEST("writeproc %d >> %s\n", 0, filepath);
    size_t twd1 = write(fd, buf, (strlen(buf)+1));
    close(fd);
}

void ErrWriteContainer65(const char *filepath)
{
    int fd = open(filepath, O_WRONLY);
    char buf[CONFIG_LEN];
    sprintf(buf, "%d", 65);
    PRINTTEST("writeproc %d >> %s\n", 66, filepath);
    size_t twd2 = write(fd, buf, (strlen(buf)+1));
    close(fd);
}

static UINT32 TestCase(void)
{
    const int CONFIG_FILE_LEN = 1024;
    char szFile[CONFIG_FILE_LEN] = {0};
    for (int i = 0; i < MAX_CONTAINER_NUM; i++) {
        sprintf(szFile, "/proc/sys/user/%s", arryEntries[i].c_str());
        WriteContainer(szFile, nInitArry[i]);
        ReadContainer(szFile, nInitArry[i]);
    }

    for (int i = 0; i < MAX_CONTAINER_NUM; i++) {
        sprintf(szFile, "/proc/sys/user/%s", arryEntries[i].c_str());
        ErrWriteContainer0(szFile);
        ReadContainer(szFile, nInitArry[i]);

        ErrWriteContainer65(szFile);
        ReadContainer(szFile, nInitArry[i]);
    }
    return 0;
}

void ItProcessFs006(void)
{
    TEST_ADD_CASE("IT_PROCESS_FS_006", TestCase, TEST_POSIX, TEST_CONTAINER, TEST_LEVEL0, TEST_FUNCTION);
}

